/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.conasinf.sofia.model.dao.Impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import com.conasinf.sofia.model.dao.GenericDAO;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import util.HibernateUtil;

/**
 *
 * @author JuanFernando
 * @param <T>
 * @param <ID>
 */
public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {

    @Override
    public T create() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void save(T entity) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        String msg;
        try {
            session.beginTransaction();
            session.save(entity);
            session.getTransaction().commit();
            msg = "Registro creado correctamente";
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            msg = "Error al crear el registro";
        }
        FacesMessage Message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Adicion",msg);
        FacesContext.getCurrentInstance().addMessage(null, Message);
    }

    @Override
    public void update(T entity) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        String msg;
        try {
            session.beginTransaction();
            session.update(entity);
            session.getTransaction().commit();
            msg = "Registro actualizado correctamente";
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            msg = "Error al actualizar el registro";
        }
        FacesMessage Message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Modificacion",msg);
        FacesContext.getCurrentInstance().addMessage(null, Message);
    }

    @Override
    public T get(ID id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void delete(ID id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public T findOne() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<T> findAll(String sql) {
        List objeto = null;

        Session sesion = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            sesion.beginTransaction();
            objeto = sesion.createQuery(sql).list();
            sesion.beginTransaction().commit();
        } catch (HibernateException e) {
            sesion.beginTransaction().rollback();
        }
        return (List<T>) objeto;
    }

    private Class<T> getEntityClass() {
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

}